﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.editorControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using umbraco.NodeFactory;
using System.Web.SessionState;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic.property;
using umbraco.interfaces;
using ExtendedWorkFlow.Helpers;
using ExtendedWorkFlow.Commands;

namespace ExtendedWorkFlow.Commands
{

    public class WorkFlowCommandBinding
    {

        #region Variables

        Document _Document;
        IWorkFlowCommand _CommandObject;
        Node _WorkFlowCommand;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor to create workflow command object
        /// </summary>
        /// <param name="CommandObject"></param>
        /// <param name="Document"></param>
        /// <param name="WorkFlowCommand"></param>
        public WorkFlowCommandBinding(IWorkFlowCommand CommandObject, Document Document, Node WorkFlowCommand)
        {
            this._CommandObject = CommandObject;
            this._Document = Document;
            this._WorkFlowCommand = WorkFlowCommand;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Document the workflow is associated with
        /// </summary>
        public Document Document { get { return this._Document; } }

        /// <summary>
        /// Command object of IWorkFlowCommand interface
        /// </summary>
        public IWorkFlowCommand CommandObject { get { return this._CommandObject; } }

        /// <summary>
        /// WorkFlowCommand node
        /// </summary>
        public Node WorkFlowCommand { get { return this._WorkFlowCommand; } }

        #endregion

        #region Public Functions

        /// <summary>
        /// DoAction performs action on the command object if the Document, CommandObject and WorkFlowCommand exists
        /// </summary>
        /// <returns>Bool indicating if successful</returns>
        public bool DoAction()
        {
            if (this.CommandObject != null &&
                this.Document != null &&
                this.WorkFlowCommand != null)
            {
                string strParameters =
                    (this.WorkFlowCommand.Properties["parameters"] != null ? this.WorkFlowCommand.Properties["parameters"].Value : string.Empty);

                return this.CommandObject.RunCommand(
                    strParameters,
                    this._Document,
                    this._WorkFlowCommand);
            }

            return false;
        }

        #endregion

    }

}
